/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license.txt -file, included in
 * the distribution.
 */

#ifndef PurchasableItemBase_H
#define PurchasableItemBase_H

#include <QObject>
#include <iapclient.h>
#include <QVariant>
#include <QImage>

class DeclarativePurchaseListModel;

class PurchasableItemBase : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QImage image READ stateIcon)// NOTIFY imageChanged)

public:
    explicit PurchasableItemBase(DeclarativePurchaseListModel& model, QString& productID, QString& productUrl);
    virtual ~PurchasableItemBase();

signals:

public slots:

protected slots:
    /*
     * In-Application Purchase specific slots
     *
     * Slots matching the signals of the In-Application Purchase API, allowing
     * the application to receive callbacks
     */
    void productDataReceived( int requestId, QString status, IAPClient::ProductDataHash productData );
    void purchaseCompleted( int requestId, QString status, QString purchaseTicket );
    void purchaseFlowFinished( int requestId );
    void restorationCompleted( int requestId, QString status, QString purchaseTicket );
    void restorationFlowFinished( int requestId );

public:
    bool isMetaDataKnown() const;
    bool isBusy();
    bool isActivated() const;

    void purchase();
    void restore();

    enum Roles {
        ProductIdRole = Qt::UserRole+1,
        DataReqResultRole,
        TitleRole,
        ShortDescriptionRole,
        LongDescriptionRole,
        PriceRole,
        DrmProtectionRole,

        IsMetadataKnown,
        IsBusy,
        IsActivated
    };

    virtual QImage& stateIcon() const;

    virtual QVariant metadata(int role) const;
    bool setMetadata(const QVariant & value, int role);

    /**
      * checks whether 'this' is included to a given list
      */
    bool isIncluded(IAPClient::ProductDataList& list);

    static QString getTicketDir();

protected:
    void fetchMetadata();
    bool setBusy();
    virtual void setUnlocked(QString* purchaseTicket) = 0;
    virtual bool isPurchased() = 0;
    /**
    * these methods below related to purchase ticket
    * are only for IAP in test mode
    * normally we don't need to operate or keep tickets
    * because purchase verification is done :
    * 1) transparently by system for DRM protected items
    * 2) and by third party server for non-protected items via ticket verification API
    *
    * Tickets are transient data that can be requested any time from OVI Store for a certain
    * product id.
    *
    * if it is decided that keeping tickets in the application private directory
    * does not bring vulnerability issue this functionality may remain
    **/
    void saveTicket(QString* purchaseTicket);
    bool readTicket();
    void deactivate();// used to purge item

private:
    QString getTicketUri();

protected:
    //data
    IAPClient::ProductData          m_productMetadata;
    DeclarativePurchaseListModel    *m_model;  //does not own
    int                             m_requestId;
    bool                            m_isBusy;
    QString                         m_productUrl;
    bool                            m_isKnown;

    QImage                          *m_unlocked_icon;
    QImage                          *m_buy_icon;
    QImage                          *m_notready_icon;
};

#endif // PurchasableItemBase_H
